/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package bpm.editor
{
	/*******************************************
	 **** huangjixin,2012-11-19,下午3:26:13 **
	 **** 请一句话表述该类主要作用  **
	 *******************************************/
	
	import bpm.graphic.Graph;
	import bpm.graphic.Node;
	import bpm.graphic.Renderer;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.FlexGlobals;
	import mx.core.IVisualElementContainer;
	
	import spark.components.supportClasses.SkinnableComponent;
	import spark.filters.GlowFilter;
	
	[SkinState("highlighted")]
	[SkinState("selected")]
	[SkinState("selectedHighlighted")]
	public class Adorner extends SkinnableComponent
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		[SkinPart]
		public var filter:GlowFilter = new GlowFilter(0xFF0000FF,1,10,10);
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _adornedObject:Renderer;

		private var _editor: DiagramEditor;

		private var _graph : Graph;
		
		private var _highlighted:Boolean;
		
		private var _selected:Boolean;
		
		private var _isMouseOver:Boolean;

		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function Adorner(adornedObject : Renderer)
		{
			super();
			this._adornedObject = adornedObject;
			this._graph = this._adornedObject.parent as Graph;
			this._adornedObject.addEventListener(MouseEvent.MOUSE_DOWN,onAdornerMouseDown);
			this._adornedObject.addEventListener(MouseEvent.MOUSE_OVER,onAdornerMouseOver);
			this._adornedObject.addEventListener(MouseEvent.MOUSE_OUT,onAdornerMouseOut);
			this._adornedObject.addEventListener("isSelectedChange",onSelectionChange);
			this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
		}
		
		
		public function get graph():Graph
		{
			return _graph;
		}

		public function get editor():DiagramEditor
		{
			return _editor;
		}
		
		public function set editor(value:DiagramEditor):void
		{
			_editor = value;
		}
		
		public function get adornedObject():Renderer
		{
			return _adornedObject;
		}
		
		public function get highlighted():Boolean
		{
			return _highlighted;
		}
		
		public function set highlighted(value:Boolean):void
		{
			_highlighted = value;
		}
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		protected function onAdornerMouseOut(event:MouseEvent):void
		{
//			event.stopPropagation();
			_isMouseOver = false;
			invalidateSkinState();
		}
		
		protected function onAdornerMouseOver(event:MouseEvent):void
		{
			event.stopPropagation();
			_isMouseOver = true;
			invalidateSkinState();
		}
		
		protected function onSelectionChange(event:Event):void
		{
			invalidateSkinState();
		}
		
		private function onAddedToStage(event:Event):void
		{
			var rect:Rectangle = getAdornerRectangle(editor.adornersGroup);
			/*var point:Point = new Point(adornedObject.x,adornedObject.y);
			point = adornedObject.parent.localToGlobal(point);
			point = graph.globalToLocal(point);*/
			this.x = rect.x;
			this.y = rect.y;
			this.width = rect.width;
			this.height = rect.height;
		}
		
		private function onAdornerMouseDown(event:MouseEvent):void
		{
			var rect:Rectangle = getAdornerRectangle(editor.adornersGroup);
			this.x = rect.x;
			this.y = rect.y;
			this.width = rect.width;
			this.height = rect.height;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onStageMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,onStageMouseUp);
			invalidateSkinState();
		}
		
		private function onStageMouseMove(event:MouseEvent):void
		{
			var rect:Rectangle = getAdornerRectangle(editor.adornersGroup);
			this.x = rect.x;
			this.y = rect.y;
			this.width = rect.width;
			this.height = rect.height;
		}
		
		private function onStageMouseUp(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onStageMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,onStageMouseUp);
		}
		
		public function getEditableTextParts():Vector.<Object>
		{
			return null;
		}//getEditableTextParts():Vector.<Object>结束
		
		public function getText(textPart:Object):String
		{
			return "";
		}//getText(textPart:Object):String结束
		
		public function getTextBounds(textPart:Object, targetCoordinateSpace:DisplayObject):Rectangle
		{
			return new Rectangle();
		}//getTextBounds(textPart:Object, targetCoordinateSpace:DisplayObject):Rectangle结束
		
		public function setText(textPart:Object, newText:String):void
		{
			
		}//setText(textPart:Object, newText:String):void结束
		
		//-------------------------------------------------------
		// 以下函数子类必须根据需要覆盖
		//-------------------------------------------------------
		public function cleanup():void
		{
			if(FlexGlobals.topLevelApplication.contains(this)){
				IVisualElementContainer(this.parent).removeElement(this);
			}
		}//cleanup():void结束
		
		protected function getAdornerRectangle(targetCoordinateSpace:DisplayObject):Rectangle
		{
			return adornedObject.getBounds(targetCoordinateSpace);
		}//getAdornerRectangle(targetCoordinateSpace:DisplayObject):Rectangle结束
		
		protected function handleDragged(handle:DisplayObject, event:MouseEvent, dx:Number, dy:Number):void
		{
			
		}//handleDragged(handle:DisplayObject, event:MouseEvent, dx:Number, dy:Number):void结束
		
		protected function handlePressed(handle:DisplayObject, event:MouseEvent):void
		{
			
		}//handlePressed(handle:DisplayObject, event:MouseEvent):void结束
		
		protected function handleReleased(handle:DisplayObject, event:MouseEvent):void
		{
			
		}//handleReleased(handle:DisplayObject, event:MouseEvent):void结束
		
		protected function isHandle(part:Object):Boolean
		{
			return false;
		}//isHandle(part:Object):Boolean结束
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override protected function getCurrentSkinState():String
		{
			if (adornedObject.isSelected){
				/*filter.alpha = 1;
				adornedObject.filters= [filter];*/
				
				return "selectedHighlighted";
			}else if (!adornedObject.isSelected && _isMouseOver){
//				filter.alpha = 0.5;
				adornedObject.filters= [filter];
				return "highlighted";
			}else if (!adornedObject.isSelected && !_isMouseOver){
				adornedObject.filters= [];
				return "selected";
			}else{
				adornedObject.filters= [];
				return "selected";
			}
			
			return super.getCurrentSkinState();
		}
	}//类结束
}//包结束